package com.zjhn.ds.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zjhn.ds.common.BizResult;
import com.zjhn.ds.domain.dto.pad.GroupInfoDto;
import com.zjhn.ds.domain.dto.pad.GroupRelDto;
import com.zjhn.ds.domain.entity.jlw.Group;
import com.zjhn.ds.domain.entity.jlw.GroupSpinner;
import com.zjhn.ds.domain.entity.jlw.GroupStation;
import com.zjhn.ds.domain.entity.jlw.Spinner;
import com.zjhn.ds.mapper.GroupMapper;
import com.zjhn.ds.mapper.GroupSpinnerMapper;
import com.zjhn.ds.mapper.GroupStationMapper;
import com.zjhn.ds.mapper.SpinnerMapper;
import com.zjhn.ds.param.AddGroupParam;
import com.zjhn.ds.param.GroupBindParam;
import com.zjhn.ds.param.GroupUnBindParam;
import com.zjhn.ds.service.GroupService;
import com.zjhn.ds.service.PersonnelService;
import com.zjhn.ds.utils.CommonUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @BelongsProject: ds_jlw
 * @BelongsPackage: com.zjhn.ds.service.impl
 * @Author: sxt
 * @CreateTime: 2023-09-22  10:04
 * @Description:
 * @Version: 1.0
 */
@Service
public class PersonnelServiceImpl implements PersonnelService {

    @Resource
    private CommonUtil commonUtil;

    @Resource
    private GroupService groupService;

    @Resource
    private GroupMapper groupMapper;

    @Resource
    private GroupStationMapper groupStationMapper;

    @Resource
    private GroupSpinnerMapper groupSpinnerMapper;

    @Resource
    private SpinnerMapper spinnerMapper;


    /**
     * 获取分组信息
     *
     * @param workshopId
     * @return
     */
    @Override
    public BizResult<List<GroupInfoDto>> getGroupList(String workshopId) {
        List<GroupInfoDto> res = new ArrayList<>();
        List<Group> groups = groupMapper.selectList(new QueryWrapper<Group>()
                .eq("workshop_id", workshopId)
                .orderByAsc("create_time")
        );
        if (groups.size() > 0)
            res.addAll(groups.stream().map(c -> GroupInfoDto.builder().groupId(c.getId()).groupName(c.getGroupName()).build()).collect(Collectors.toList()));
        return BizResult.success(res);
    }

    /**
     * 获取分组关联信息
     *
     * @param groupId
     * @return
     */
    @Override
    public BizResult<GroupRelDto> getGroupRel(String groupId) {
        GroupRelDto res = GroupRelDto.builder().build();
        List<GroupRelDto.GroupStationDto> stationList = new ArrayList<>();
        List<GroupRelDto.GroupSpinnerDto> spinnerList = new ArrayList<>();

        List<GroupStation> groupStations = groupStationMapper.selectList(
                new QueryWrapper<GroupStation>().eq("group_id", groupId));
        if (groupStations.size() > 0) {
            List<GroupRelDto.GroupStationDto> collect = groupStations.stream().map(c -> GroupRelDto.GroupStationDto.builder()
                    .stationId(c.getStationId())
                    .stationName(commonUtil.getStationById(c.getStationId()).getStationName())
                    .stationCode(commonUtil.getStationById(c.getStationId()).getStationName())
                    .build()).collect(Collectors.toList());

            // 排序
            stationList.addAll(
                    collect.stream().sorted(
                            Comparator.comparing(c -> Integer.parseInt(c.getStationName()))
                    ).collect(Collectors.toList())
            );
        }

        List<GroupSpinner> groupSpinners = groupSpinnerMapper.selectList(
            new QueryWrapper<GroupSpinner>().eq("group_id", groupId)
                    .orderByAsc("create_time")
        );
        if (groupSpinners.size() > 0) {
            List<String> spinnerIds = groupSpinners.stream().map(GroupSpinner::getSpinnerId).collect(Collectors.toList());
            List<Spinner> spinners = spinnerMapper.selectBatchIds(spinnerIds);
            Map<String, String> collect = spinners.stream().collect(Collectors.toMap(Spinner::getId, Spinner::getOperateId));

            spinnerList.addAll(
                    groupSpinners.stream().map(c -> GroupRelDto.GroupSpinnerDto.builder()
                            .spinnerId(c.getSpinnerId())
                            .realName(commonUtil.getOperatorById(collect.get(c.getSpinnerId())).getRealName())
                            .build()).collect(Collectors.toList())
            );
        }

        res.setStationList(stationList);
        res.setSpinnerList(spinnerList);
        return BizResult.success(res);
    }

    /**
     * 新增组别
     * @param param
     * @return
     */
    @Override
    public BizResult<?> addUpdateGroup(AddGroupParam param) {
        return groupService.addUpdateGroup(param);
    }

    /**
     * 删除组别
     * @param groupId
     * @return
     */
    @Override
    public BizResult<?> removeGroup(String groupId) {
        return groupService.removeGroup(groupId);
    }

    /**
     * 组别批量绑定挡车工或工位
     * @param param
     * @return
     */
    @Override
    public BizResult<?> groupBind(GroupBindParam param) {
        return groupService.groupBind(param);
    }

    /**
     * 组别解绑挡车工或工位
     * @param param
     * @return
     */
    @Override
    public BizResult<?> groupUnBind(GroupUnBindParam param) {
        return groupService.groupUnBind(param);
    }
}
